package co.edu.usbcali.egrapico.vista.ambient;

public class LPaneRestrictionF extends javax.swing.JPanel {

    private java.util.Vector figures;
    private java.util.Vector Undo;
    private boolean ban = false, sel = false, sel2 = false;
    private boolean Igual = true, Op = true, desdeOp = false, desdeIgual = false;
    private java.awt.Dimension area;
    private java.awt.Component dragComponent;
    private int xAdjustment, xPresed;
    private int yAdjustment, yPresed;

    /**
     * Creates a new instance of LPane
     */
    public LPaneRestrictionF() {
        figures = new java.util.Vector();
        Undo = new java.util.Vector();
        area = new java.awt.Dimension(0, 0);
        setLayout(null);
        setBackground(new java.awt.Color(255, 255, 255));

        addMouseListener(new java.awt.event.MouseAdapter() {

            public void mousePressed(java.awt.event.MouseEvent evt) {
                LPMousePressed(evt);
            }

            public void mouseReleased(java.awt.event.MouseEvent evt) {
                LPMouseReleased(evt);
            }

            public void mouseClicked(java.awt.event.MouseEvent evt) {
                LPMouseClicked(evt);
            }
        });
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {

            public void mouseDragged(java.awt.event.MouseEvent evt) {
                LPMouseDragged(evt);
            }

            public void mouseMoved(java.awt.event.MouseEvent evt) {
                LPMouseMoved(evt);
            }
        });

    }

    public void loadLPane(LPaneRestrictionF ob) {
        Igual = ob.Igual;
        Op = ob.Op;
        desdeOp = ob.desdeOp;
        desdeIgual = ob.desdeIgual;
        area = ob.area;
        for (int i = 0; i < ob.figures.size(); i++) {
            if (ob.figures.get(i) instanceof LabelIdeOpe) {

                LabelIdeOpe label = new LabelIdeOpe();
                label.setIcon(((LabelIdeOpe) ob.figures.get(i)).getIcon());
                label.setHorizontalTextPosition(javax.swing.JLabel.CENTER);
                label.setVerticalTextPosition(javax.swing.JLabel.BOTTOM);
                label.setText(((LabelIdeOpe) ob.figures.get(i)).getText());
                label.setTextPane(((LabelIdeOpe) ob.figures.get(i)).getTextPane());
                label.setSimbol(((LabelIdeOpe) ob.figures.get(i)).getSimbol());
                label.setId(((LabelIdeOpe) ob.figures.get(i)).getId());
                label.setLocation(((LabelIdeOpe) ob.figures.get(i)).getLocation());
                label.setSize(((LabelIdeOpe) ob.figures.get(i)).getSize());
                label.setIsOfObject(((LabelIdeOpe) ob.figures.get(i)).getisObject());
                label.setnumLine(((LabelIdeOpe) ob.figures.get(i)).getnumLine());
                figures.add(label);
                add(label);
            } else {
                figures.add(ob.figures.get(i));
            }
        }
        revalidate();
        repaint();
    }

    public java.awt.Dimension areaLPane() {
        return area;
    }

    public void setAreaLP(java.awt.Dimension ar) {
        area = ar;
    }
    //methods

    public void addMyListener() {

        addMouseListener(new java.awt.event.MouseAdapter() {

            public void mousePressed(java.awt.event.MouseEvent evt) {
                LPMousePressed(evt);
            }

            public void mouseReleased(java.awt.event.MouseEvent evt) {
                LPMouseReleased(evt);
            }

            public void mouseClicked(java.awt.event.MouseEvent evt) {
                LPMouseClicked(evt);
            }
        });
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {

            public void mouseDragged(java.awt.event.MouseEvent evt) {
                LPMouseDragged(evt);
            }

            public void mouseMoved(java.awt.event.MouseEvent evt) {
                LPMouseMoved(evt);
            }
        });
    }

    private void LPMouseMoved(java.awt.event.MouseEvent evt) {
        java.awt.Component c = findComponentAt(evt.getX(), evt.getY());
        if (c instanceof LabelIdeOpe) {
            setToolTipText(((LabelIdeOpe) c).getTextToolTip());
        } else {
            setToolTipText("Ambient");
        }
    }

    private void LPMouseClicked(java.awt.event.MouseEvent evt) {
        java.awt.Component c = findComponentAt(evt.getX(), evt.getY());
        if (evt.getButton() == java.awt.event.MouseEvent.BUTTON3) {
            if (c instanceof LabelIdeOpe) {
                ((LabelIdeOpe) c).mostrarMenu(evt.getComponent(), evt.getX(), evt.getY());
            }
        }
        if (evt.getClickCount() == 2) {

            if (c instanceof LabelIdeOpe) {
                if (((LabelIdeOpe) c).isCreate() == false) {
                    ((LabelIdeOpe) c).Ladicionar();
                } else {
                    ((LabelIdeOpe) c).mostrarPanel();
                }
            }
        }
    }

    private void LPMousePressed(java.awt.event.MouseEvent evt) {
        dragComponent = null;
        java.awt.Component c = findComponentAt(evt.getX(), evt.getY());
        if (c instanceof LabelIdeOpe) {
            if (((LabelIdeOpe) c).getIcon().toString().endsWith("value.gif")) {
                ((LabelIdeOpe) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/value1.gif")));
            } else if (((LabelIdeOpe) c).getIcon().toString().endsWith("variable.gif")) {
                ((LabelIdeOpe) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/variable1.gif")));
            } else if (((LabelIdeOpe) c).getIcon().toString().endsWith("argument.gif")) {
                ((LabelIdeOpe) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/argumente1.gif")));
            } else if (((LabelIdeOpe) c).getIcon().toString().endsWith("forwardOperator.gif")) {
                ((LabelIdeOpe) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/forwardOperator1.gif")));
            } else if (((LabelIdeOpe) c).getIcon().toString().endsWith("equal.gif")) {
                ((LabelIdeOpe) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/equal1.gif")));
            } else if (((LabelIdeOpe) c).getIcon().toString().endsWith("distinct.gif")) {
                ((LabelIdeOpe) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/distinct1.gif")));
            } else if (((LabelIdeOpe) c).getIcon().toString().endsWith("greater.gif")) {
                ((LabelIdeOpe) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/greater1.gif")));
            } else if (((LabelIdeOpe) c).getIcon().toString().endsWith("greaterEqual.gif")) {
                ((LabelIdeOpe) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/greaterEqual1.gif")));
            } else if (((LabelIdeOpe) c).getIcon().toString().endsWith("minor.gif")) {
                ((LabelIdeOpe) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/minor1.gif")));
            } else if (((LabelIdeOpe) c).getIcon().toString().endsWith("minorEqual.gif")) {
                ((LabelIdeOpe) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/minorEqual1.gif")));
            } else if (((LabelIdeOpe) c).getIcon().toString().endsWith("add.gif")) {
                ((LabelIdeOpe) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/add1.gif")));
            } else if (((LabelIdeOpe) c).getIcon().toString().endsWith("multiply.gif")) {
                ((LabelIdeOpe) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/multiply1.gif")));
            } else if (((LabelIdeOpe) c).getIcon().toString().endsWith("substract.gif")) {
                ((LabelIdeOpe) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/substract1.gif")));
            } else if (((LabelIdeOpe) c).getIcon().toString().endsWith("divide.gif")) {
                ((LabelIdeOpe) c).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/divide1.gif")));
            }
            ApIcons(c);
        }
        dragComponent = c;
        xAdjustment = dragComponent.getLocation().x - evt.getX();
        yAdjustment = dragComponent.getLocation().y - evt.getY();
        dragComponent.setLocation(evt.getX() + xAdjustment, evt.getY() + yAdjustment);
        if (evt.getButton() == java.awt.event.MouseEvent.BUTTON1) {
            ban = true;
        } else if (evt.getButton() == java.awt.event.MouseEvent.BUTTON3) {
            ban = false;
            xPresed = dragComponent.getLocation().x + dragComponent.getWidth() / 2;
            yPresed = dragComponent.getLocation().y + dragComponent.getHeight() / 2;
            //condiciones de creacion de Linea
            if (!(((LabelIdeOpe) c).getIcon().toString().endsWith("value1.gif")
                    || ((LabelIdeOpe) c).getIcon().toString().endsWith("variable1.gif")
                    || ((LabelIdeOpe) c).getIcon().toString().endsWith("argumente1.gif")
                    || ((LabelIdeOpe) c).getIcon().toString().endsWith("forwardOperator1.gif"))) {
                if (((LabelIdeOpe) c).getnumLine() < 2) {
                    sel = true;
                    sel2 = true;
                }
            }
            //************************creacion de Linea
            if (sel) {
                sel = false;
                if (((LabelIdeOpe) dragComponent).getId().equals("id1") && Igual) {
                    Linea myline = new Linea(xPresed, yPresed, java.awt.Color.RED);
                    figures.add(myline);
                    desdeIgual = true;
                } else if (((LabelIdeOpe) dragComponent).getId().equals("id1") && !Igual && ((LabelIdeOpe) dragComponent).getnumLine() < 2) {
                    Linea myline = new Linea(xPresed, yPresed, java.awt.Color.BLACK);
                    figures.add(myline);
                    desdeIgual = true;
                } else if (((LabelIdeOpe) dragComponent).getId().equals("id3") && Op) {
                    Linea myline = new Linea(xPresed, yPresed, java.awt.Color.BLUE);
                    figures.add(myline);
                    desdeOp = true;
                } else if (((LabelIdeOpe) dragComponent).getId().equals("id3") && !Op && ((LabelIdeOpe) dragComponent).getnumLine() < 2) {
                    Linea myline = new Linea(xPresed, yPresed, java.awt.Color.BLACK);
                    figures.add(myline);
                    desdeOp = true;
                }
            }
            //***********************fin crecionde Linea
        }
    }

    public void ApIcons(java.awt.Component c) {
        for (int i = 0; i < getComponentCount(); i++) {
            if (!getComponent(i).equals(c)) {
                if (getComponent(i) instanceof LabelIdeOpe) {
                    if (((LabelIdeOpe) getComponent(i)).getIcon().toString().endsWith("value1.gif")) {
                        ((LabelIdeOpe) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/value.gif")));
                    } else if (((LabelIdeOpe) getComponent(i)).getIcon().toString().endsWith("variable1.gif")) {
                        ((LabelIdeOpe) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/variable.gif")));
                    } else if (((LabelIdeOpe) getComponent(i)).getIcon().toString().endsWith("argumente1.gif")) {
                        ((LabelIdeOpe) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/argument.gif")));
                    } else if (((LabelIdeOpe) getComponent(i)).getIcon().toString().endsWith("forwardOperator1.gif")) {
                        ((LabelIdeOpe) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/forwardOperator.gif")));
                    } else if (((LabelIdeOpe) getComponent(i)).getIcon().toString().endsWith("equal1.gif")) {
                        ((LabelIdeOpe) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/equal.gif")));
                    } else if (((LabelIdeOpe) getComponent(i)).getIcon().toString().endsWith("distinct1.gif")) {
                        ((LabelIdeOpe) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/distinct.gif")));
                    } else if (((LabelIdeOpe) getComponent(i)).getIcon().toString().endsWith("greater1.gif")) {
                        ((LabelIdeOpe) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/greater.gif")));
                    } else if (((LabelIdeOpe) getComponent(i)).getIcon().toString().endsWith("greaterEqual1.gif")) {
                        ((LabelIdeOpe) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/greaterEqual.gif")));
                    } else if (((LabelIdeOpe) getComponent(i)).getIcon().toString().endsWith("minor1.gif")) {
                        ((LabelIdeOpe) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/minor.gif")));
                    } else if (((LabelIdeOpe) getComponent(i)).getIcon().toString().endsWith("minorEqual1.gif")) {
                        ((LabelIdeOpe) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/minorEqual.gif")));
                    } else if (((LabelIdeOpe) getComponent(i)).getIcon().toString().endsWith("add1.gif")) {
                        ((LabelIdeOpe) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/add.gif")));
                    } else if (((LabelIdeOpe) getComponent(i)).getIcon().toString().endsWith("substract1.gif")) {
                        ((LabelIdeOpe) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/substract.gif")));
                    } else if (((LabelIdeOpe) getComponent(i)).getIcon().toString().endsWith("multiply1.gif")) {
                        ((LabelIdeOpe) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/multiply.gif")));
                    } else if (((LabelIdeOpe) getComponent(i)).getIcon().toString().endsWith("divide1.gif")) {
                        ((LabelIdeOpe) getComponent(i)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/divide.gif")));
                    }
                }
            }
        }
    }

    private void LPMouseDragged(java.awt.event.MouseEvent evt) {
        int evtX = evt.getX();
        int evtY = evt.getY();
        if (dragComponent == null) {
            return;
        }
        if (ban) {
            if (evtX + xAdjustment < 0) {
                evtX = 0;
                xAdjustment = 0;
            }
            if (evtY + yAdjustment < 0) {
                evtY = 0;
                yAdjustment = 0;
            }
            Forma myshape;
            for (int i = 0; i < figures.size(); i++) {
                if (figures.get(i) instanceof Forma) {
                    myshape = (Forma) figures.get(i);
                    if (myshape.getPoint1().x == dragComponent.getX() + dragComponent.getWidth() / 2 && myshape.getPoint1().y == dragComponent.getY() + dragComponent.getHeight() / 2) {
                        myshape.setPoint1(evtX + xAdjustment + dragComponent.getWidth() / 2, evtY + yAdjustment + dragComponent.getHeight() / 2);
                        repaint();
                    } else if (myshape.getPoint2().x == dragComponent.getX() + dragComponent.getWidth() / 2 && myshape.getPoint2().y == dragComponent.getY() + dragComponent.getHeight() / 2) {
                        myshape.setPoint2(evtX + xAdjustment + dragComponent.getWidth() / 2, evtY + yAdjustment + dragComponent.getHeight() / 2);
                        repaint();
                    }
                }
            }
            dragComponent.setLocation(evtX + xAdjustment, evtY + yAdjustment);
            /**
             * ***********************verificacion del scroll***************
             */
            final int W = 100;
            final int H = 100;
            int x = evtX - W / 2;
            int y = evtY - H / 2;
            if (x < 0) {
                x = 0;
            }
            if (y < 0) {
                y = 0;
            }
            java.awt.Rectangle rect = new java.awt.Rectangle(x, y, W, H);
            scrollRectToVisible(rect);
            int this_width = (x + W + 2);
            if (this_width > area.width) {
                area.width = this_width;
            }
            int this_height = (y + H + 2);
            if (this_height > area.height) {
                area.height = this_height;
            }
            setPreferredSize(area);
            revalidate();
        } else {//si es boton derecho 

            if (sel2) {
                Forma myshape = (Forma) figures.get(figures.size() - 1);
                if (((LabelIdeOpe) findComponentAt(myshape.getPoint1())).getnumLine() < 2) {
                    myshape.setPoint2(evt.getX(), evt.getY());
                    repaint();
                }
            }
        }
    }

    private void LPMouseReleased(java.awt.event.MouseEvent evt) {
        dragComponent = null;
        java.awt.Component c = findComponentAt(evt.getX(), evt.getY());
        if (sel2) {
            sel2 = false;
            if (c instanceof LabelIdeOpe) {
                Forma myshape = (Forma) figures.get(figures.size() - 1);
                myshape.setPoint2(c.getX() + c.getWidth() / 2, c.getY() + c.getHeight() / 2);
                if (myshape.getPoint1().equals(myshape.getPoint2())) { //Esta en el mismo punto
                    if ((((LabelIdeOpe) findComponentAt(myshape.getPoint1())).getId().equals("id1") && ((LabelIdeOpe) findComponentAt(myshape.getPoint1())).getnumLine() < 2)
                            || (((LabelIdeOpe) findComponentAt(myshape.getPoint1())).getId().equals("id3") && ((LabelIdeOpe) findComponentAt(myshape.getPoint1())).getnumLine() < 2)) {
                        figures.remove(figures.size() - 1);
                    }
                    desdeIgual = false;
                    desdeOp = false;
                    repaint();
                    return;
                }
                int num = 0;
                for (int i = 0; i < figures.size(); i++) {
                    if (figures.get(i) instanceof Forma) {
                        if (myshape.getPoint1().equals(((Forma) figures.get(i)).getPoint1())) {
                            if (myshape.getPoint2().equals(((Forma) figures.get(i)).getPoint2())) {
                                num++;
                            }
                        }
                    }
                }
                if (num > 1) {
                    figures.remove(figures.size() - 1);
                } else {

                    if (((LabelIdeOpe) c).getIcon().toString().endsWith("value.gif")
                            || ((LabelIdeOpe) c).getIcon().toString().endsWith("variable.gif")
                            || ((LabelIdeOpe) c).getIcon().toString().endsWith("argument.gif")
                            || ((LabelIdeOpe) c).getIcon().toString().endsWith("forwardOperator.gif")
                            || ((LabelIdeOpe) c).getId().equals("id3")) {
                        if (Igual && desdeIgual && !((LabelIdeOpe) c).getId().equals("id3")) {
                            if (((LabelIdeOpe) c).getId().length() > 0) {
                                figures.remove(figures.size() - 1);
                            } else {
                                ((LabelIdeOpe) c).setId("id2");
                                Igual = false;
                                ((LabelIdeOpe) findComponentAt(myshape.getPoint1())).addLine();
                            }
                        } else if (Igual && desdeIgual && ((LabelIdeOpe) c).getId().equals("id3")) {
                            figures.remove(figures.size() - 1);
                        } else if (!Igual && desdeIgual) {
                            if (((LabelIdeOpe) c).getId().equals("id3")) {
                                ((LabelIdeOpe) findComponentAt(myshape.getPoint1())).addLine();
                            } else if (((PanelRestrictionF) getParent().getParent().getParent().getParent().getParent().getParent()).getOperadoresMath() < 1) {
                                ((LabelIdeOpe) c).setId("id3");
                                ((LabelIdeOpe) findComponentAt(myshape.getPoint1())).addLine();
                            } else {
                                figures.remove(figures.size() - 1);
                            }
                        }
                        if (Op && desdeOp) {
                            if (((LabelIdeOpe) c).getId().length() > 0) {
                                figures.remove(figures.size() - 1);
                            } else {
                                ((LabelIdeOpe) c).setId("id4");
                                Op = false;
                                ((LabelIdeOpe) findComponentAt(myshape.getPoint1())).addLine();
                            }
                        } else if (!Op && desdeOp) {
                            if (((LabelIdeOpe) c).getId().length() > 0) {
                                figures.remove(figures.size() - 1);
                            } else {
                                ((LabelIdeOpe) c).setId("id5");
                                ((LabelIdeOpe) findComponentAt(myshape.getPoint1())).addLine();
                            }
                        }

                    }//fin if 
                    else if (((LabelIdeOpe) c).getId().equals("id1") && desdeOp) {
                        figures.remove(figures.size() - 1);
                    }

                }

            } else {
                if (figures.lastElement() instanceof Linea) {
                    figures.remove(figures.size() - 1);
                }
            }
        }
        desdeIgual = false;
        desdeOp = false;
        repaint();

    }

    public void paintComponent(java.awt.Graphics g) {
        super.paintComponent(g);
        int i = 0;
        for (int j = figures.size(); i < j; i++) {
            if (figures.get(i) instanceof Forma) {
                Forma myshape = (Forma) figures.get(i);
                if (myshape.getPoint2().getX() != 0 && myshape.getPoint2().getY() != 0) {
                    myshape.draw(g);
                }
            }
        }
    }

    public void addLabeltoVector(LabelIdeOpe labe) {
        figures.add(labe);
    }

    public void addLabel(LabelIdeOpe labe) {
        add(labe);
        revalidate();
        repaint();
    }

    public void Undo() {
        Undo.add(figures.lastElement());
        int i = figures.size();
        if (figures.get(i - 1) instanceof LabelIdeOpe) {
            if (((LabelIdeOpe) figures.get(i - 1)).getIcon().toString().endsWith("value.gif")) {
                ((PanelRestrictionF) getParent().getParent().getParent().getParent().getParent().getParent()).resValue();
            } else if (((LabelIdeOpe) figures.get(i - 1)).getIcon().toString().endsWith("variable.gif")) {
                ((PanelRestrictionF) getParent().getParent().getParent().getParent().getParent().getParent()).resVariable();
            } else if (((LabelIdeOpe) figures.get(i - 1)).getIcon().toString().endsWith("argument.gif")) {
                ((PanelRestrictionF) getParent().getParent().getParent().getParent().getParent().getParent()).resArgumento();
            } else if (((LabelIdeOpe) figures.get(i - 1)).getIcon().toString().endsWith("forwardOperator.gif")) {
                ((PanelRestrictionF) getParent().getParent().getParent().getParent().getParent().getParent()).resForwardOperador();
            } else if (((LabelIdeOpe) figures.get(i - 1)).getIcon().toString().endsWith("add.gif")) {
                ((PanelRestrictionF) getParent().getParent().getParent().getParent().getParent().getParent()).resSuma();
            } else if (((LabelIdeOpe) figures.get(i - 1)).getIcon().toString().endsWith("substract.gif")) {
                ((PanelRestrictionF) getParent().getParent().getParent().getParent().getParent().getParent()).resResta();
            } else if (((LabelIdeOpe) figures.get(i - 1)).getIcon().toString().endsWith("multiply.gif")) {
                ((PanelRestrictionF) getParent().getParent().getParent().getParent().getParent().getParent()).resMul();
            } else if (((LabelIdeOpe) figures.get(i - 1)).getIcon().toString().endsWith("divide.gif")) {
                ((PanelRestrictionF) getParent().getParent().getParent().getParent().getParent().getParent()).resDiv();
            } else if (((LabelIdeOpe) figures.get(i - 1)).getIcon().toString().endsWith("equal.gif")) {
                ((PanelRestrictionF) getParent().getParent().getParent().getParent().getParent().getParent()).resIgual();
            } else if (((LabelIdeOpe) figures.get(i - 1)).getIcon().toString().endsWith("greater.gif")) {
                ((PanelRestrictionF) getParent().getParent().getParent().getParent().getParent().getParent()).resMayor();
            } else if (((LabelIdeOpe) figures.get(i - 1)).getIcon().toString().endsWith("greaterEqual.gif")) {
                ((PanelRestrictionF) getParent().getParent().getParent().getParent().getParent().getParent()).resMayorIgual();
            } else if (((LabelIdeOpe) figures.get(i - 1)).getIcon().toString().endsWith("minor.gif")) {
                ((PanelRestrictionF) getParent().getParent().getParent().getParent().getParent().getParent()).resMenor();
            } else if (((LabelIdeOpe) figures.get(i - 1)).getIcon().toString().endsWith("minorEqual.gif")) {
                ((PanelRestrictionF) getParent().getParent().getParent().getParent().getParent().getParent()).resMenorIgual();
            } else if (((LabelIdeOpe) figures.get(i - 1)).getIcon().toString().endsWith("distinct.gif")) {
                ((PanelRestrictionF) getParent().getParent().getParent().getParent().getParent().getParent()).resDiferente();
            }

            remove((java.awt.Component) figures.get(i - 1));
        } else {

            Forma myshape = (Forma) figures.get(figures.size() - 1);
            if (((LabelIdeOpe) findComponentAt(myshape.getPoint1())).getId().equals("id1")) {
                ((LabelIdeOpe) findComponentAt(myshape.getPoint1())).removeLine();
                if (((LabelIdeOpe) findComponentAt(myshape.getPoint1())).getnumLine() == 0) {
                    Igual = true;
                } else if (((LabelIdeOpe) findComponentAt(myshape.getPoint1())).getnumLine() == 1) {
                    Igual = false;
                }
                if (((LabelIdeOpe) findComponentAt(myshape.getPoint2())).getIcon().toString().endsWith("value.gif")
                        || ((LabelIdeOpe) findComponentAt(myshape.getPoint2())).getIcon().toString().endsWith("variable.gif")
                        || ((LabelIdeOpe) findComponentAt(myshape.getPoint2())).getIcon().toString().endsWith("argument.gif")
                        || ((LabelIdeOpe) findComponentAt(myshape.getPoint2())).getIcon().toString().endsWith("forwardOperator.gif")) {
                    ((LabelIdeOpe) findComponentAt(myshape.getPoint2())).setId("");
                }
            } else if (((LabelIdeOpe) findComponentAt(myshape.getPoint1())).getId().equals("id3")) {
                ((LabelIdeOpe) findComponentAt(myshape.getPoint1())).removeLine();
                if (((LabelIdeOpe) findComponentAt(myshape.getPoint1())).getnumLine() == 0) {
                    Op = true;
                } else if (((LabelIdeOpe) findComponentAt(myshape.getPoint1())).getnumLine() == 1) {
                    Op = false;
                }
                if (((LabelIdeOpe) findComponentAt(myshape.getPoint2())).getIcon().toString().endsWith("value.gif")
                        || ((LabelIdeOpe) findComponentAt(myshape.getPoint2())).getIcon().toString().endsWith("variable.gif")
                        || ((LabelIdeOpe) findComponentAt(myshape.getPoint2())).getIcon().toString().endsWith("argument.gif")
                        || ((LabelIdeOpe) findComponentAt(myshape.getPoint2())).getIcon().toString().endsWith("forwardOperator.gif")) {
                    ((LabelIdeOpe) findComponentAt(myshape.getPoint2())).setId("");
                }
            }
        }
        figures.remove(i - 1);
        repaint();
    }

    public int getUndoSize() {
        return Undo.size();
    }

    public void Redo() {
        if (Undo.lastElement() instanceof LabelIdeOpe) {
            add((java.awt.Component) Undo.lastElement());
        }
        figures.add(Undo.lastElement());
        int i = Undo.size();
        Undo.remove(i - 1);
        repaint();
    }

    public int getRedoSize() {
        return figures.size();
    }

    public void removerLineas(LabelIdeOpe label) {
        for (int i = 0; i < figures.size(); i++) {
            if (figures.get(i) instanceof Forma) {
                Forma myshape = (Forma) figures.get(i);
                if (((LabelIdeOpe) findComponentAt(myshape.getPoint1())).equals(label) && label.getId().equals("id1")) {
                    ((LabelIdeOpe) findComponentAt(myshape.getPoint1())).removeLine();
                    if (((LabelIdeOpe) findComponentAt(myshape.getPoint1())).getnumLine() == 0) {
                        Igual = true;
                    }
                    if (((LabelIdeOpe) findComponentAt(myshape.getPoint2())).getIcon().toString().endsWith("value.gif")
                            || ((LabelIdeOpe) findComponentAt(myshape.getPoint2())).getIcon().toString().endsWith("variable.gif")
                            || ((LabelIdeOpe) findComponentAt(myshape.getPoint2())).getIcon().toString().endsWith("argument.gif")
                            || ((LabelIdeOpe) findComponentAt(myshape.getPoint2())).getIcon().toString().endsWith("forwardOperator.gif")) {
                        ((LabelIdeOpe) findComponentAt(myshape.getPoint2())).setId("");
                    }
                    figures.remove(i);
                    i--;
                } else if (((LabelIdeOpe) findComponentAt(myshape.getPoint1())).equals(label) && label.getId().equals("id3")) {
                    ((LabelIdeOpe) findComponentAt(myshape.getPoint1())).removeLine();
                    if (((LabelIdeOpe) findComponentAt(myshape.getPoint1())).getnumLine() == 0) {
                        Op = true;
                    }
                    if (((LabelIdeOpe) findComponentAt(myshape.getPoint2())).getIcon().toString().endsWith("value.gif")
                            || ((LabelIdeOpe) findComponentAt(myshape.getPoint2())).getIcon().toString().endsWith("variable.gif")
                            || ((LabelIdeOpe) findComponentAt(myshape.getPoint2())).getIcon().toString().endsWith("argument.gif")
                            || ((LabelIdeOpe) findComponentAt(myshape.getPoint2())).getIcon().toString().endsWith("forwardOperator.gif")) {
                        ((LabelIdeOpe) findComponentAt(myshape.getPoint2())).setId("");
                    }
                    figures.remove(i);
                    i--;
                } else if (((LabelIdeOpe) findComponentAt(myshape.getPoint1())).getId().equals("id1")
                        && ((LabelIdeOpe) findComponentAt(myshape.getPoint2())).equals(label) && label.getId().equals("id3")) {
                    ((LabelIdeOpe) findComponentAt(myshape.getPoint1())).removeLine();
                    if (((LabelIdeOpe) findComponentAt(myshape.getPoint2())).getIcon().toString().endsWith("value.gif")
                            || ((LabelIdeOpe) findComponentAt(myshape.getPoint2())).getIcon().toString().endsWith("variable.gif")
                            || ((LabelIdeOpe) findComponentAt(myshape.getPoint2())).getIcon().toString().endsWith("argument.gif")
                            || ((LabelIdeOpe) findComponentAt(myshape.getPoint2())).getIcon().toString().endsWith("forwardOperator.gif")) {
                        ((LabelIdeOpe) findComponentAt(myshape.getPoint2())).setId("");
                    }
                    figures.remove(i);
                    i--;
                } else if (((LabelIdeOpe) findComponentAt(myshape.getPoint1())).getId().equals("id1")
                        && ((LabelIdeOpe) findComponentAt(myshape.getPoint2())).equals(label) && label.getId().equals("id2")) {
                    ((LabelIdeOpe) findComponentAt(myshape.getPoint1())).removeLine();
                    Igual = true;
                    figures.remove(i);
                    i--;
                } else if (((LabelIdeOpe) findComponentAt(myshape.getPoint1())).getId().equals("id3")
                        && ((LabelIdeOpe) findComponentAt(myshape.getPoint2())).equals(label) && label.getId().equals("id4")) {
                    ((LabelIdeOpe) findComponentAt(myshape.getPoint1())).removeLine();
                    Op = true;
                    figures.remove(i);
                    i--;
                } else if (((LabelIdeOpe) findComponentAt(myshape.getPoint1())).getId().equals("id3")
                        && ((LabelIdeOpe) findComponentAt(myshape.getPoint2())).equals(label) && label.getId().equals("id5")) {
                    ((LabelIdeOpe) findComponentAt(myshape.getPoint1())).removeLine();
                    figures.remove(i);
                    i--;
                }
            }
        }
        figures.remove(label);
    }

    public void makeLine() {
    }
}